
import android.os.Handler
import android.os.Looper
import com.dcjt.firehotcarpad.entity.CarCurrentDetailEntity
import com.dcjt.firehotcarpad.entity.MapEvent
import com.dcjt.firehotcarpad.fragment.DisasterListInfoEntity
import com.dcjt.firehotcarpad.fragment.WaterSourceEntity
import com.dcjt.firehotcarpad.utils.MarkerToMapType
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors


/**
 * Created by cj on 2021/2/22.
 * Email:codesexy@163.com
 * Function:
 * desc:为整个应用提供车辆状态服务
 */
class MapOperationEventPool private constructor(
    private val mDispatchEngine: ExecutorService = Executors.newSingleThreadExecutor(),
    private val mHandler: Handler = Handler(Looper.getMainLooper())
) {

    /**
     * 当前车辆信息
     */
    private var currentStateDetail: CarCurrentDetailEntity? = null

    private var observers: MutableList<MapOperationEventObserver> = mutableListOf()

    companion object {
        private var instance: MapOperationEventPool? = null
            get() {
                if (field == null) {
                    field = MapOperationEventPool()
                }
                return field
            }

        fun get(): MapOperationEventPool {
            return instance!!
        }
    }


    /**
     * 开始分发消息
     */
    public fun sendDispatch(entity: MapEvent) {
        mDispatchEngine.submit {
            try {
                observers.forEach {
                    mHandler.post {
                        try {
                            it.onMapOperationEvent(entity)
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
    }

    /**
     * 开始分发消息--周边水源
     */
    public fun sendAroundWaterMakersDispatch(
        points: List<WaterSourceEntity>?,
        type: MarkerToMapType,page :Int=0
    ) {
        mDispatchEngine.submit {
            try {
                observers.forEach {
                    mHandler.post {
                        try {
                            if (type == MarkerToMapType.MARKER_ADD) {
                                it.onAddAroundWaterMakers(points,page)
                            } else if (type == MarkerToMapType.MARKER_HINT) {
                                it.onHintAroundWaterMakers()
                            } else if (type == MarkerToMapType.MARKER_SHOW) {
                                it.onShowAroundWaterMakers()
                            } else if (type == MarkerToMapType.MARKER_REMOVE) {
                                it.onRemoveMakers()
                            } else if (type == MarkerToMapType.MARKER_IMITATE_CLICK) {
                                points?.get(0)?.let { point -> it.onImitateClickMarker(point) }
                            }
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
    }

    /**
     * 开始分发消息--周边水源
     */
    public fun sendDisasterMakersDispatch(
        points: List<DisasterListInfoEntity>?,
        type: MarkerToMapType,page:Int = 0
    ) {
        mDispatchEngine.submit {
            try {
                observers.forEach {
                    mHandler.post {
                        try {
                            if (type == MarkerToMapType.MARKER_ADD) {
                                it.onAddDisasterMakers(points,page)
                            } else if (type == MarkerToMapType.MARKER_HINT) {
                                it.onHintDisasterMakers()
                            } else if (type == MarkerToMapType.MARKER_SHOW) {
                                it.onShowDisasterMakers()
                            }
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
    }


    fun registerMapOperationEventObserver(observer: MapOperationEventObserver) {
        observers.add(observer)
    }


    fun unRegisterMapOperationEventObserver(observer: MapOperationEventObserver) {
        observers.remove(observer)
    }


    interface MapOperationEventObserver {
        fun onMapOperationEvent(event: MapEvent)
        fun onAddAroundWaterMakers(points: List<WaterSourceEntity>?,page:Int)
        fun onHintAroundWaterMakers()
        fun onShowAroundWaterMakers()
        fun onAddDisasterMakers(points: List<DisasterListInfoEntity>?,page:Int)
        fun onHintDisasterMakers()
        fun onShowDisasterMakers()
        fun onRemoveMakers()
        fun onImitateClickMarker(points: WaterSourceEntity) {}
    }


}